//
//  GroupParser.m
//  iengage
//
//  Created by Luis Ziliani on 2/12/09.
//  Copyright 2009 Manas. All rights reserved.
//

#import "GroupParser.h"
#import "Util.h"

#define DateFormat					@"yyyy-MM-dd HH:mm ZZZZ"
#define RootElement					@"iengage"
#define GroupsElement				@"groups"
#define GroupElement				@"group"
#define UsersElement				@"users"
#define UserElement					@"user"
#define UserImageElement			@"imageData"
#define GroupNameAttribute			@"name"
#define UserLoginAttribute			@"login"
#define UserDisplayNameAttribute	@"displayName"
#define UserLastMessageAttribute	@"lastMessage"
#define UserLastMessageDateAttribute @"lastMessageDate"
#define UserLatitudeAttribute		@"latitude"
#define UserLongitudeAttribute		@"longitude"


@implementation GroupParser

@synthesize users;

- (id) init {
	self = [super init];
	users = [[UserCollection alloc] init];
	dateFormatter = [[NSDateFormatter alloc] init];
	imageData = [[NSMutableString alloc] init];
	[dateFormatter setDateFormat:DateFormat];
	
	return self;
}

- (void) dealloc {
	[users release];
	[dateFormatter release];
	[imageData release];
	[super dealloc];
}

- (BOOL) parse: (NSString*) file {

    NSURL *url = [NSURL fileURLWithPath:file];
	
	NSXMLParser* addressParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
	[addressParser setDelegate:self];
	[addressParser parse];
	[addressParser release];
	
	return !errorOccurred;
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
	if (errorOccurred) {
		return ;
	}
	
	if ([elementName compare:RootElement] == 0){
		lastTag = GeoChatTag;
	}
	else if ([elementName compare:GroupsElement] == 0){
		lastTag = GroupsTag;
	}
	else if ([elementName compare:GroupElement] == 0){
		lastTag = GroupTag;
	}
	else if ([elementName compare:UsersElement] == 0){
		lastTag = UsersTag;
	}	
	else if ([elementName compare:UserElement] == 0){
		lastTag = UserTag;
		User* user = [[User alloc] init];
		
		[user setDisplayName: [attributeDict objectForKey:UserDisplayNameAttribute]];
		[user setLatitude: [[attributeDict objectForKey:UserLatitudeAttribute] doubleValue]];
		[user setLongitude: [[attributeDict objectForKey:UserLongitudeAttribute] doubleValue]];
		[user setLastMessage: [attributeDict objectForKey:UserLastMessageAttribute]];
		[user setLogin: [attributeDict objectForKey:UserLoginAttribute]];
		NSString* lastMessageDate = [attributeDict objectForKey:UserLastMessageDateAttribute];
		if ([NSString isNullOrEmpty:lastMessageDate]) {
			[user setLastMessageDate:nil];
		}
		else {
			[user setLastMessageDate: [dateFormatter dateFromString:lastMessageDate]];
		}
		
		// sometimes the displayname can be empty. we fill it with the user name
		if ([NSString isNullOrEmpty: [user displayName]]) {
			[user setDisplayName:[user login]];
		}
		
		[self.users addUser: user];
		[user release];
	}
	else if ([elementName compare:UserImageElement] == 0){
		if (lastTag != UserTag || [self.users count] == 0) {
			errorOccurred = YES;
			return;
		}
		lastTag = UserImageTag;
		[imageData release];
		imageData = [[NSMutableString alloc] init];
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	if (errorOccurred) {
		return ;
	}
	
	if ([elementName compare:UserImageElement] == 0){
		if (lastTag != UserImageTag) {
			errorOccurred = YES;
			return;
		}
		User * lastUser = [self.users lastUser];
		[lastUser setImageData: imageData];
		lastTag = UserTag;
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	if (errorOccurred) {
		return ;
	}
	
	if (lastTag == UserImageTag){
		[imageData appendString:string];
	}
}

		
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
	errorOccurred = YES;
}

@end
